package ferp.android.activities;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import ferp.android.GUI;
import ferp.android.R;
import ferp.android.activities.preferences.Preferences;
import ferp.android.ads.interstitial.InterstitialAdManager;
import ferp.android.managers.ProfileManager;
import ferp.core.player.Profile;

/**
 * User: olegs
 * Date: 11/29/12 9:32 PM
 */
public class Results extends TabActivity
{
  public static void show(Activity activity)
  {
    Intent intent = new Intent(activity.getApplicationContext(), Results.class);
    Bundle bundle = new Bundle();

    bundle.putBoolean(ENTRANCE, true);
    intent.putExtras(bundle);

    activity.startActivity(intent);
  }

  @Override
  public void onBackPressed()
  {
    if (getIntent().getBooleanExtra(ENTRANCE, false))
    {
      startActivity(new Intent(getApplicationContext(), Entrance.class));
      // show interstitial ad on game over
      InterstitialAdManager.show(this);
    }

    super.onBackPressed();
  }

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    ProfileManager pm = ProfileManager.regular(this);
    Profile p = pm.getCurrentProfile();

    // set correct orientation depends on if running on tablet or not
    GUI.setOrientation(this, p);

    super.onCreate(savedInstanceState);

    Preferences.setLocale(this, p);

    setContentView(R.layout.score_tab_host);

    // resolve tab host
    host = getTabHost();

    //host.getTabWidget().setDividerDrawable(R.drawable.bg_leaderboard_header);

    // setup tabs
    setupTab(R.string.statistics, R.drawable.statistics, SELECTED_TAB_TEXT_COLOR, Statistics.class);
    setupTab(R.string.leaderboard_top10, R.drawable.top_10_disable, UNSELECTED_TAB_TEXT_COLOR, Leaderboard.class);

    host.setOnTabChangedListener(new TabHost.OnTabChangeListener()
    {
      public void onTabChanged(String s)
      {
        int tabsCount = host.getTabWidget().getTabCount();

        for (int i = 0; i < tabsCount; ++i)
        {
          View tab = host.getTabWidget().getChildTabViewAt(i);

          TextView title = (TextView)tab.findViewById(R.id.score_tab_title);

          CharSequence text = title.getText();

          boolean selected = text.equals(s);
          boolean statistics = text.equals(getResources().getString(R.string.statistics));

          title.setTextColor(selected ? SELECTED_TAB_TEXT_COLOR : UNSELECTED_TAB_TEXT_COLOR);

          ImageView icon = (ImageView)tab.findViewById(R.id.score_tab_icon);

          if (selected)
            icon.setImageDrawable(getResources().getDrawable(statistics ? R.drawable.statistics : R.drawable.top_10));
          else
            icon.setImageDrawable(getResources().getDrawable(statistics ? R.drawable.statistics_disable : R.drawable.top_10_disable));
        }
      }
    });
  }

  private void setupTab(int labelID, int drawableID, int color, Class<?> activityClass)
  {
    String label = getResources().getString(labelID);

    TabHost.TabSpec spec = host.newTabSpec(label);

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.score_tab_indicator, getTabWidget(), false);

    TextView title = (TextView)tabIndicator.findViewById(R.id.score_tab_title);

    title.setTypeface(GUI.Font.BOLD);
    title.setTextColor(color);
    title.setText(label);

    ImageView icon = (ImageView)tabIndicator.findViewById(R.id.score_tab_icon);

    icon.setImageResource(drawableID);

    spec.setIndicator(tabIndicator);

    spec.setContent(new Intent(this, activityClass));

    host.addTab(spec);
  }

  // selected and not selected tabs text colors
  private final static int SELECTED_TAB_TEXT_COLOR   = 0xFFFF8C00;
  private final static int UNSELECTED_TAB_TEXT_COLOR = 0x77FFFFFF;

  // entrance intent flag
  private final static String ENTRANCE = "entrance";

  // tab host
  private TabHost host;
}
